package com.oscar.ehome.view.pageview;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.loginbean.UserBean;
import com.oscar.ehome.bean.pagebean.InuranceMessageBean;
import com.oscar.ehome.bean.pagebean.WorkMessageBean;
import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.pageview.view.InsuranceView;

import net.tycmc.bulb.androidstandard.utils.JsonUtils;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/8/31
 * 填写保险档案
 */

public class InsurawriteActivity extends BasesActivity implements TextWatcher {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * 公司
     */
    @BindView(R.id.company_edtext)
    EditText companyEdtext;
    /**
     * 保单号
     */
    @BindView(R.id.baodannumber_edtext)
    EditText baodannumberEdtext;
    /**
     * 保单状态
     */
    @BindView(R.id.baodantype_edtext)
    EditText baodantypeEdtext;
    /**
     * 投保人
     */
    @BindView(R.id.toub_people_edtext)
    EditText toubPeopleEdtext;
    /**
     * 投保人电话
     */
    @BindView(R.id.toub_phone_edtext)
    EditText toubPhoneEdtext;
    /**
     * 被保险人
     */
    @BindView(R.id.bei_insuar_pople_edtext)
    EditText beiInsuarPopleEdtext;
    /**
     * 受益人
     */
    @BindView(R.id.shouyi_pople_edtext)
    EditText shouyiPopleEdtext;
    /**
     * 划款银行
     */
    @BindView(R.id.blank_edtext)
    EditText blankEdtext;
    /**
     * 划款帐号
     */
    @BindView(R.id.blank_number_edtext)
    EditText blankNumberEdtext;
    /**
     * 下一步
     */
    @BindView(R.id.next_buton)
    Button nextButon;

    Map<String, Object> map = new HashMap<String, Object>();
    String add = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insuarwrite);
        ButterKnife.bind(this);
        setColor();
        initView();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    /**
     * 改变颜色
     */
    public void setColor() {
        TintManager.setTint(nextButon, Color.parseColor(SystemConfigFactory.getInstance(InsurawriteActivity.this).getSystemConfig().getColor()));
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(InsurawriteActivity.this).getSystemConfig().getColor()));
    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText(getString(R.string.insurance_text));

        add = getIntent().getStringExtra("add");
        if ("2".equals(add)) {
            setMessage();
        }

        toubPhoneEdtext.addTextChangedListener(this);
    }

    /**
     * 填充信息
     */
    public void setMessage() {
        String message = SystemConfigFactory.getInstance(InsurawriteActivity.this).getSystemConfig().getInaurance();
        InuranceMessageBean bean = new Gson().fromJson(message, InuranceMessageBean.class);
        if (bean != null) {
            companyEdtext.setText(bean.getCompanyName());
            baodannumberEdtext.setText(bean.getPolicyNumber());
            baodantypeEdtext.setText(bean.getPolicyStatus());
            toubPeopleEdtext.setText(bean.getInsuredPerson());
            toubPhoneEdtext.setText(bean.getInsuredPersonMobile());
            beiInsuarPopleEdtext.setText(bean.getInsured());
            shouyiPopleEdtext.setText(bean.getBeneficiaries());
            blankEdtext.setText(bean.getTransferBank());
            blankNumberEdtext.setText(bean.getTransferAccountNumber());
        }
    }

    /**
     * 保存信息
     */
    public void initSave() {
        String company = companyEdtext.getText().toString();
        String bd_number = baodannumberEdtext.getText().toString();
        String bd_type = baodantypeEdtext.getText().toString();
        String tb_people = toubPeopleEdtext.getText().toString();
        String tb_phone = toubPhoneEdtext.getText().toString();
        String bins_people = beiInsuarPopleEdtext.getText().toString();
        String sy_people = shouyiPopleEdtext.getText().toString();
        String blank = blankEdtext.getText().toString();
        String blank_number = blankNumberEdtext.getText().toString();
        if (StringUtils.isNotEmpty(company) && StringUtils.isNotEmpty(bd_number) && StringUtils.isNotEmpty(bd_type) && StringUtils.isNotEmpty(tb_people) &&
                StringUtils.isNotEmpty(tb_phone) && StringUtils.isNotEmpty(bins_people) && StringUtils.isNotEmpty(sy_people) && StringUtils.isNotEmpty(blank)
                && StringUtils.isNotEmpty(blank_number)) {

            Map<String, String> map = new HashMap<String, String>();
            map.put("companyName", company);
            map.put("policyNumber", bd_number);
            map.put("policyStatus", bd_type);
            map.put("insuredPerson", tb_people);
            map.put("insuredPersonMobile", tb_phone);
            map.put("insured", bins_people);
            map.put("beneficiaries", sy_people);
            map.put("transferBank", blank);
            map.put("transferAccountNumber", blank_number);

            if ("2".equals(add)) {
                Intent intent = new Intent(this, InsurWritesActivity.class);
                intent.putExtra("mesage", JsonUtils.toJson(map));
                intent.putExtra("id", "2");
                intent.putExtra("addm", "2");
                startActivity(intent);
            } else {
                Intent intent = new Intent(this, InsurWritesActivity.class);
                intent.putExtra("mesage", JsonUtils.toJson(map));
                intent.putExtra("id", "2");
                intent.putExtra("addm", "1");
                startActivity(intent);
            }
        } else {
            ToastUtil.show(this, "请填写信息");
        }
    }

    @OnClick({R.id.title_layout_left, R.id.next_buton})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.next_buton:
                initSave();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
